package org.dkakunsi.aplikasitoko.form.admin;

import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import org.dkakunsi.aplikasitoko.data.entity.Supplier;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.form.Desktop;
import org.dkakunsi.aplikasitoko.service.DataService;


public class DataSupplier extends FormData {

	private static final long serialVersionUID = 1L;
	
	private String[] kolom = {"ID Supplier", "Nama Supplier"};
	private Supplier supplier;
	private String sign;

	/**
	 * create frame with no desktop initialized
	 * @param judul
	 */
	public DataSupplier(String judul) {
		super(judul);
		setter();
	}

	/**
	 * create frame with desktop initialized
	 * @param judul
	 * @param desktop
	 */
	public DataSupplier(String judul, Desktop desktop) {
		super(judul, desktop);
		setter();
	}
	
	private void setter(){
		this.setSupplier();
		loadInitialize();
		this.textField.requestFocus();
		ds = new DataService();
	}

	@Override
	protected void add() {
		reset();
		this.textField.setEnabled(true);
		this.textField_1.setEnabled(true);
		sign = "ADD";
	}

	@Override
	protected void update() {
		sign = "UPDATE";
		this.textField.setEnabled(true);
		this.textField_1.setEnabled(true);
	}

	@Override
	protected void save() {
		switch(sign){
			case "ADD" :
				supplier = new Supplier();
				supplier.setKode(textField.getText());
				supplier.setNama(textField_1.getText());

				boolean canAdd = isExistInList(this.lsLoaded, this.supplier, true);

				if(canAdd == true){
					lsAdded.add(supplier);
					load(null, supplier);
				} else {
					JOptionPane.showMessageDialog(this, "Kesalahan pada ID/KODE Supplier", "Duplikasi Entry", JOptionPane.ERROR_MESSAGE);
				}
				break;
			case "UPDATE" :
				supplier.setNama(textField_1.getText());

				boolean canUpdate = this.isExistInList(this.lsLoaded, this.supplier, false);
			
				if(canUpdate == true){
					boolean existInAdd = this.isExistInList(this.lsAdded, this.supplier, false);
				
					if(existInAdd == true){
						Iterator<Object> itA = lsAdded.iterator();
						while(itA.hasNext()){
							Supplier supplierFound = (Supplier) itA.next();
							if(supplierFound.getKode().equals(supplier.getKode())){
								lsAdded.remove(supplierFound);
								lsAdded.add(supplier);
								load(supplierFound, supplier);
								break;
							}
						}
					} else {
						boolean existInUpdate = this.isExistInList(this.lsUpdated, this.supplier, false);

						if(existInUpdate == true){
							Iterator<Object> itU = lsUpdated.iterator();
							while(itU.hasNext()){
								Supplier supplierFound = (Supplier) itU.next();
								if(supplierFound.getKode().equals(supplier.getKode())){
									lsUpdated.remove(supplierFound);
									lsUpdated.add(supplier);
									load(supplierFound, supplier);
									break;
								}
							}
						} else {
							Iterator<Object> itL = lsLoaded.iterator();
							while(itL.hasNext()){
								Supplier supplierFound = (Supplier) itL.next();
								if(supplierFound.getKode().equals(supplier.getKode())){
									lsUpdated.add(supplier);
									load(supplierFound, supplier);
									break;
								}
							}
						}
					}
				}
				break;
			default :
				break;
		}

		sign = "";
		supplier = null;
		reset();
	}

	@Override
	protected void delete() {
		boolean canDelete = this.isExistInList(this.lsLoaded, supplier, false);
		
		if(canDelete == true){
			boolean existInAdd = this.isExistInList(this.lsAdded, supplier, false);
			
			if(existInAdd == true){
				Iterator<Object> itA = lsAdded.iterator();
				while(itA.hasNext()){
					Supplier supplierFound = (Supplier) itA.next();
					if(supplierFound.getKode().equals(supplier.getKode())){
						lsAdded.remove(supplierFound);
						load(supplierFound, null);
						break;
					}
				}
			} else {
				boolean existInUpdate = this.isExistInList(this.lsUpdated, supplier, false);
				
				if(existInUpdate == true){
					Iterator<Object> itU = lsUpdated.iterator();
					while(itU.hasNext()){
						Supplier supplierFound = (Supplier) itU.next();
						if(supplierFound.getKode().equals(supplier.getKode())){
							lsUpdated.remove(supplierFound);
							load(supplierFound, null);
							break;
						}
					}
				} else {
					lsDeleted.add(supplier);
					load(supplier, null);
				}
			}
		}
	}

	@Override
	protected void reset() {
		sign = "";
		this.textField.setText("");
		this.textField_1.setText("");
		this.textField_2.setText("");
		this.textField_3.setText("");
		this.textField_4.setText("");

		this.textField.setEnabled(true);
		this.textField_1.setEnabled(false);

		this.textField.requestFocus();
	}

	@Override
	protected void search(String id) throws ApplicationDataException {
		boolean found = false;
		String idKasir = this.textField.getText();
		Iterator<Object> itL = lsLoaded.iterator();

		while(itL.hasNext()){
			supplier = (Supplier) itL.next();
			
			if(supplier.getKode().equals(idKasir)){
				found = true;
				break;
			}
		}

		if(found == true){
			this.textField.setText(supplier.getKode());
			this.textField_1.setText(supplier.getNama());
		} else {
			throw new ApplicationDataException( "Kesalahan KODE" );
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void loadInitialize() {
		try {
			lsLoaded = (List<Object>) ds.getAll("SUPPLIER");
			loadView();
		} catch (ApplicationDataException e) {
			System.out.println(e.getMessage() + "\n@ DataBarang.load()");
		}
	}

	@Override
	protected void loadView() {
		this.setTableModel(kolom);

		Iterator<Object> itL = lsLoaded.iterator();
		while(itL.hasNext()){
			Supplier supplier = (Supplier) itL.next();

			try {
				String[] data = { supplier.getKode(), supplier.getNama() };
				tabModel.addRow(data);
			} catch (NullPointerException exc){
			} catch (ClassCastException exc){
				JOptionPane.showMessageDialog(this, "ClassCastException @DataSupplier.loadView()", "Debugging", JOptionPane.DEFAULT_OPTION);
			}
		}
	}

}
